import React, { Component } from 'react';

class Boundary extends Component {
  constructor() {
    super();
    this.state = {
      hasError: false
    }
  }
  componentDidCatch() {
    // 捕获错误的钩子函数，会在子组件报错时执行
    this.setState({
      hasError: true
    })
  }
  render() {
    return (
      <>
        {/* 错误边界，使报错的组件只影响当前组件，不影响其他内容的展示 */}
        {this.state.hasError? '报错了，请联系客服...': this.props.children}
      </>
    );
  }
}

export default Boundary;
